<?php
require_once dirname(__FILE__).'/../../rightMenuWidget/lib/RightMenuHelper.class.php';
require_once dirname(__FILE__).'/../../banners/lib/BannersHelper.class.php';
require_once dirname(__FILE__).'/../../homepage/lib/HomepageHelper.class.php';

class eventsScheduleComponents extends myComponents{
	public function executeRender(){


		$this->section = $this->getSection();


		if($this->getAction()->getRoute()->getProductObject()){
			return $this->getAction()->renderPartial(
					'eventsSchedule/eventPage',
						array(
							'event' => $this->getAction()->getRoute()->getProductObject(),
							'cover' => $this->getCover()->getFile(),
							'section' => $this->section,
							'galleryFirstBanner' => $this->getGalleryFirstBanner(),
							'siblings' => $this->getHelper()
									->getItemChildren($this->getAction()->getRoute()->getObject(), array()),
							'bannersHelper' => new BannersHelper()
							
						)
					);
		}else{

			$this->node = $this->getAction()->getRoute()->getObject();
			$this->events = $this->getProducts();

			$this->fillSlotsBeforeRender();

			return $this->getAction()->renderPartial(
					'eventsSchedule/render',
						$this->getVarHolder()->getAll()
					);
		}
	}


	public function getSection(){
		$helper = new HomepageHelper();
		return $helper->getNodeSectionQuery($this->getRoute()->getObject())->execute()->getFirst();
	}


	public function getCover(){
		return $this->getAction()->getRoute()->getProductObject()
					->getOneProductPictureByParameter('cover');
	}

	public function getGalleryFirstBanner(){

		if($this->getPics()->count()){
			return $this->getPics()->getFirst();
		}

		return false;
	}

	public function getPics(){
		$b = $this->getAction()->getRoute()->getProductObject()
				->getBannerGroups();

		$group_ids = array(-1);

		foreach($b as $banGroup){
			$group_ids[] = $banGroup['banner_group_id'];
		}

		return Doctrine::getTable('Banner')->createQuery('sdf')
				->from('Banner b')
				->whereIn('b.banner_group_id', $group_ids)
				->execute();

	}

	public function getHelper(){
		return new RightMenuHelper();
	}

	public function getProducts(){
		//return $this->getHelper()->getItemChildren($this->getAction()->getRoute()->getObject(), array());
		$result = array();
		
		$this->getHelper()->getProductsItems($result,$this->getAction()->getRoute()->getObject());

		
		return $result;
	}

	protected function fillSlotsBeforeRender(){

		if($this->section){
			$archiveQ = StructureTable::getNodeChildrenQuery($this->section)
							->andWhere('s.parameter = ?', 'archive');

			$archive = $archiveQ->execute()->getFirst();
		}

		$this->getAction()->getPartial('eventsSchedule/archiveLink',
				array(
					'archive' => $archive
					
				)
		);
	}
}


